import type { Bean, PageBean, ResultBean, ResultBeanAsAny } from '../../interface';

export enum enumFrontPageInfoKey {
  pageSize = 'pageSize',
  current = 'current',
  total = 'total',
  totalPageSize = 'totalPageSize',
}

export enum enumBackServerPageInfoKey {
  pageSize = 'pageSize',
  current = 'pageNum',
  total = 'totalCount',
  totalPageSize = 'totalPage',
}

export interface BackServerPageInfo {
  [enumBackServerPageInfoKey.pageSize]: number;
  [enumBackServerPageInfoKey.current]: number;
  [enumBackServerPageInfoKey.total]: number;
  [enumBackServerPageInfoKey.totalPageSize]: number;
}

export function transToFrontPageInfo(serverPage: Bean) {
  const pageInfo:Bean = {};
  pageInfo[enumFrontPageInfoKey.pageSize] = serverPage[enumBackServerPageInfoKey.pageSize];
  pageInfo[enumFrontPageInfoKey.current] = serverPage[enumBackServerPageInfoKey.current];
  pageInfo[enumFrontPageInfoKey.total] = serverPage[enumBackServerPageInfoKey.total];
  pageInfo[enumFrontPageInfoKey.totalPageSize] =
    serverPage[enumBackServerPageInfoKey.totalPageSize];
  console.log(
    '🚀 ~ file: index.ts ~ line 25 ~ transToFrontPageInfo ~ serverPage, pageInfo',
    serverPage,
    pageInfo,
  );

  return pageInfo as PageBean;
}

export function transToBackServerPageInfo(frontPage: Bean) {
  const pageInfo: Bean = {};
  pageInfo[enumBackServerPageInfoKey.pageSize] = frontPage[enumFrontPageInfoKey.pageSize];
  pageInfo[enumBackServerPageInfoKey.current] = frontPage[enumFrontPageInfoKey.current];
  pageInfo[enumBackServerPageInfoKey.total] = frontPage[enumFrontPageInfoKey.total];
  pageInfo[enumBackServerPageInfoKey.totalPageSize] = frontPage[enumFrontPageInfoKey.totalPageSize];
  console.log(
    '🚀 ~ file: index.ts ~ line 35 ~ transToBackServerPageInfo ~ frontPage, pageInfo',
    frontPage,
    pageInfo,
  );
  return pageInfo as BackServerPageInfo;
}

export function calcPageInfoResult<T = Bean>(result: ResultBeanAsAny): ResultBean<T> {
  const pageInfo = transToFrontPageInfo(result.data || {});
  const { data, ...other } = result;
  const newResult: ResultBean<T> = {
    ...other,
    pageInfo,
    data,
  };
  return newResult;
}

export function calcBackServerPageInfoParams(params: Bean) {
  const newParams = {
    ...params,
  };
  Object.keys(enumFrontPageInfoKey).forEach((key) => {
    if (typeof params[key] === 'number') {
      delete newParams[key];
      // @ts-ignore
      newParams[enumBackServerPageInfoKey[key]] = params[key];
    }
  });
  console.log(
    '🚀 ~ file: index.ts ~ line 77 ~ calcBackServerPageInfoParams ~ newParams',
    newParams,
  );
  return newParams;
}
